/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchResult.java
 *
 * Created on Apr 9, 2009, 12:10:09 AM
 */

// TODO : Test SearchResult

package ui;

import documentSearching.Document;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import project3.StemInfo.StemInstance;

/**
 * <b>SearchResult</b>
 *
 * <p>This class represents the visual end for a classic presentation of a query based search item
 * (i.e. a list item) that will contain various elements such as the name of the document, sample
 * text, and a link to the document.  Instances of <code>SearchResult</code> are what will be passed
 * to <code>SearchPanel</code> as well as <code>VisualPanel</code>.
 */
public class SearchResult extends javax.swing.JPanel
{

    /** Creates new form SearchResult */
    public SearchResult()
    {
        initComponents();
    }

    public SearchResult(Document d)
    {
    	mDoc = d;
        initComponents();
        setDisplayProperties();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textPane = new javax.swing.JScrollPane();
        documentText = new javax.swing.JTextArea();
        rankField = new javax.swing.JTextField();

        documentText.setBackground(new java.awt.Color(224, 223, 227));
        documentText.setColumns(20);
        documentText.setRows(5);
        textPane.setViewportView(documentText);

        rankField.setBackground(new java.awt.Color(224, 223, 227));
        rankField.setEditable(false);
        rankField.setText("stuff");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(rankField, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textPane, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(rankField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textPane, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public void setDocument(Document doc)
    {
    	mDoc = doc;
    	setDisplayProperties();
    }

    /**
     * Returns the document associated with this search result
     * @return - <code>Document</code> used to fill out this panel
     */
    public Document getDocument()
    {
    	return new Document(mDoc);
    }

    // Sets how the SearchResult should look based on the given document
    private void setDisplayProperties()
    {
    	setBorder();
        setRank();
        setSampleText();
    }

    // Set the border to the title of the document
    private void setBorder()
    {
        if (mDoc != null)
        {
            TitledBorder border = BorderFactory.createTitledBorder(mDoc.getName());
            this.setBorder(border);
        }
    }

    // Shows the rank of the document
    private void setRank()
    {
        if (mDoc != null)
        {
            Double d = mDoc.getRank();
            rankField.setText(d.toString());
        }
    }

    // Gives the user sample text
    private void setSampleText()
    {
        ArrayList<String> indexes = mDoc.getIndexWords();

        StringBuilder builder = new StringBuilder();

        for (String word : indexes)
        {
            String spacing = " : ";
            
            builder.append(word + spacing + "\n");
            builder.append("\t" + mDoc.getPath() + "\n");

            ArrayList<StemInstance> instances = mDoc.getInstances(word);

            for (StemInstance instance : instances)
            {
	            Integer lineNumber = instance.getLineNumber();
	            String stem = instance.toString();
	            String sample = instance.getInstance();
	
	            builder.append("\t" + "Line:" + instance.getLineNumber() + "\n");
            }
        }

        documentText.setText(builder.toString());
    }
    
    @Override
    protected void paintComponent(Graphics g)
    {
    	super.paintComponent(g);
    	
    	this.setBackground(Color.WHITE);
    	
    	Dimension textBox = documentText.getSize();
    	
    	textBox.height = getSize().height - 5;
    	
    	documentText.setSize(textBox);
    	
    	this.revalidate();
    }
    
    private Document mDoc;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea documentText;
    private javax.swing.JTextField rankField;
    private javax.swing.JScrollPane textPane;
    // End of variables declaration//GEN-END:variables

}
